<template>
  <div id="userLoginPage">
    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <div class="logo-container">
            <img src="@/assets/logo.svg" alt="Logo" class="logo" />
          </div>
          <h2 class="login-title">欢迎回来</h2>
          <p class="login-subtitle">登录您的账号以继续</p>
        </div>

        <a-form class="login-form" :model="form" @submit="handleSubmit">
          <a-form-item field="userAccount">
            <a-input
              v-model="form.userAccount"
              placeholder="请输入账号"
              :style="{ width: '100%' }"
              size="large"
            >
              <template #prefix>
                <icon-user />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item field="userPassword" tooltip="密码不少于 8 位">
            <a-input-password
              v-model="form.userPassword"
              placeholder="请输入密码"
              :style="{ width: '100%' }"
              size="large"
            >
              <template #prefix>
                <icon-lock />
              </template>
            </a-input-password>
          </a-form-item>

          <div class="remember-forgot">
            <a-checkbox>记住我</a-checkbox>
            <a-link>忘记密码?</a-link>
          </div>

          <a-form-item>
            <a-button type="primary" html-type="submit" long size="large">
              登录
            </a-button>
          </a-form-item>
        </a-form>

        <div class="login-footer">
          <p>
            还没有账号?
            <a-link href="/user/register">立即注册</a-link>
          </p>
        </div>
      </div>

      <div class="login-decoration">
        <div class="decoration-content">
          <h2>智能答题系统</h2>
          <p>发现适合您的学习资源</p>
          <div class="decoration-image">
            <img src="@/assets/education.svg" alt="Education illustration" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import API from "@/api";
import { reactive } from "vue";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { userLoginUsingPost } from "@/api/userController";
import { useLoginUserStore } from "@/store/userStore";
import { IconUser, IconLock } from "@arco-design/web-vue/es/icon";

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
} as API.UserLoginRequest);

const router = useRouter();
const loginUserStore = useLoginUserStore();

const handleSubmit = async () => {
  const res = await userLoginUsingPost(form);
  if (res.data.code === 0) {
    await loginUserStore.fetchLoginUser();
    message.success("登录成功");
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登录失败，" + res.data.message);
  }
};
</script>

<style scoped>
#userLoginPage {
  height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fe;
  padding: 20px;
  box-sizing: border-box;
}

.login-container {
  display: flex;
  width: 90%;
  max-width: 900px;
  height: auto;
  min-height: 520px;
  max-height: 90vh;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.12);
  border-radius: 12px;
  overflow: hidden;
}

.login-card {
  flex: 1;
  background-color: #fff;
  padding: 32px;
  display: flex;
  flex-direction: column;
}

.login-header {
  text-align: center;
  margin-bottom: 24px;
}

.logo-container {
  margin-bottom: 20px;
}

.logo {
  height: 50px;
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--color-text-1);
  margin: 0 0 8px;
}

.login-subtitle {
  font-size: 14px;
  color: var(--color-text-3);
  margin: 0;
}

.login-form {
  flex: 1;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
  color: var(--color-text-3);
}

.login-decoration {
  flex: 1;
  background: linear-gradient(135deg, #7cb8ff, #165dff);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  color: #fff;
}

.decoration-content {
  text-align: center;
}

.decoration-content h2 {
  font-size: 26px;
  font-weight: 600;
  margin-bottom: 12px;
}

.decoration-content p {
  font-size: 15px;
  margin-bottom: 24px;
  opacity: 0.9;
}

.decoration-image img {
  width: 200px;
  height: 200px;
  filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.15));
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    height: auto;
    width: 95%;
  }

  .login-decoration {
    display: none; /* 在小屏幕上隐藏装饰部分 */
  }

  .login-card {
    padding: 24px;
  }
}
</style>
